package com.zoro.barn.commons.webcommons.exceptions;

import com.zoro.barn.commons.foundation.exceptions.BusinessException;
import com.zoro.barn.commons.foundation.exceptions.PayloadBusinessException;
import com.zoro.barn.commons.foundation.constants.web.ResponseConstants;
import com.zoro.barn.commons.webcommons.response.entity.BarnResponseEntity;
import com.zoro.barn.commons.webcommons.tools.WebContentUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * @author zhaoxingwu
 */
public class DefaultExceptionHandler {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    public ResponseEntity<BarnResponseEntity<?>> exceptionHandler(Exception e) {
        BarnResponseEntity<?> result = new BarnResponseEntity<>();
        String uri = WebContentUtil.getUri();
        String message = e.getMessage();
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        String exception = sw.toString();
        result.setStatus(ResponseConstants.STATUS_ERROR);
        result.setMessage(message);
        result.setException(exception);

        ResponseEntity<BarnResponseEntity<?>> response = new ResponseEntity<>(result, HttpStatus.INTERNAL_SERVER_ERROR);

        logger.error("request: " + uri + " has a exception", e);

        return response;
    }


    public ResponseEntity<BarnResponseEntity<?>> businessExceptionHandler(BusinessException e) {
        BarnResponseEntity<?> result = new BarnResponseEntity<>();
        String uri = WebContentUtil.getUri();
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        String exception = sw.toString();
        result.setStatus(e.getCode());
        result.setMessage(e.getMessage());
        result.setException(exception);

        ResponseEntity<BarnResponseEntity<?>> response = new ResponseEntity<>(result, HttpStatus.INTERNAL_SERVER_ERROR);

        logger.error("request: " + uri + " has a exception", e);

        return response;
    }


    /**
     * @param e exception
     * @return response
     * @see PayloadBusinessException
     */
    public ResponseEntity<BarnResponseEntity<?>> businessExceptionHandler(PayloadBusinessException e) {
        String uri = WebContentUtil.getUri();
        BarnResponseEntity<Object> result = new BarnResponseEntity<>();
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        String exception = sw.toString();
        result.setStatus(e.getCode());
        result.setMessage(e.getMessage());
        result.setException(exception);
        result.setData(e.getData());

        ResponseEntity<BarnResponseEntity<?>> response = new ResponseEntity<>(result, HttpStatus.INTERNAL_SERVER_ERROR);

        logger.error("request: " + uri + " has a exception", e);

        return response;
    }
}
